package org.uim.cricket.analyzer.analyze.impl;

import java.util.List;
import java.util.Map;

import org.uim.cricket.analyzer.analyze.Alogger;
import org.uim.cricket.analyzer.analyze.AnalyzerList;
import org.uim.cricket.analyzer.analyze.CricketMatchAnalyzable;
import org.uim.cricket.analyzer.analyze.ExecutorAnalyzings;
import org.uim.cricket.analyzer.analyze.InningMappable;
import org.uim.cricket.analyzer.analyze.PlayerInitializable;
import org.uim.cricket.entities.MatchPlayer;
import org.uim.cricket.entities.match.CricketMatch;

public class DefaultExecutorAnalyzings implements ExecutorAnalyzings {

	private AnalyzerList list = null;
	private PlayerInitializable playerInitializer = null;
	private InningMappable inningMapper = null;
	
	public DefaultExecutorAnalyzings(AnalyzerList theList, PlayerInitializable thePlayerInit, InningMappable theInnMapper) {
		this.list = theList;
		this.inningMapper = theInnMapper;
		this.playerInitializer = thePlayerInit;
	}
	
	@Override
	public Map<String, List<MatchPlayer>> execute(CricketMatch match) throws Exception {
		if (this.playerInitializer == null) {
			Alogger.error("PlayerInitializable interface can't be null!!!");
			Alogger.error("No execution is processed. Quiting.");
			return null;
		}
		if (this.inningMapper == null) {
			Alogger.error("InningMapper interface can't be null!!!");
			Alogger.error("No execution is processed. Quiting.");
			return null;
		}
		
		Alogger.debug("  Mapping players...");
		Map<String, List<MatchPlayer>> allPlayers = this.playerInitializer.initMatchPlayers(inningMapper, match);
		Alogger.debug("  Completed.");
		
		if (list.isEmpty()) {
			Alogger.warn("No analyzers are found in list!");
			return allPlayers;
		}
		
		Alogger.debug("Initializing executing analyzers...");
		while (!list.isEmpty()) {
			
			CricketMatchAnalyzable currAnalyzer = list.popAnalyzer();
			if (currAnalyzer == null) continue;
			
			Alogger.debug("  Executing...");
			Alogger.debug("    - [" + currAnalyzer.getClass().getName() + "]...");
			
			currAnalyzer.doAnalyze(match, allPlayers);
			
		}
		Alogger.debug("Completed executings.");
		
		return allPlayers;
	}

	public void setPlayerInitializer(PlayerInitializable playerInitializer) {
		this.playerInitializer = playerInitializer;
	}
	
	public void setList(AnalyzerList list) {
		this.list = list;
	}
	
	public void setInningMapper(InningMappable inningMapper) {
		this.inningMapper = inningMapper;
	}
	
	public PlayerInitializable getPlayerInitializer() {
		return playerInitializer;
	}
	
	public AnalyzerList getList() {
		return list;
	}
	
	public InningMappable getInningMapper() {
		return inningMapper;
	}
	 
}
